package com.boot.main.config.configDemo;

import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
import cn.hutool.extra.spring.SpringUtil;
import com.boot.main.entity.CcomLog;
import com.boot.main.service.impl.LogService;

public class LogFilter extends Filter<LoggingEvent> {
// https://blog.csdn.net/wujiangbo520/article/details/131305002
// https://www.cnblogs.com/xiaoqigui/archive/2022/10/14/16790630.html
    public LogService logService = null;
    @Override
    public FilterReply decide(LoggingEvent event){
        String loggerName = event.getLoggerName();
        if(loggerName.startsWith("com.boot.main")){
            CcomLog log = new CcomLog();
            log.setLog_time(event.getTimeStamp());
            log.setLog_thread(event.getThreadName());
            log.setLog_class(loggerName);
            log.setLog_level(event.getLevel().levelStr);
            log.setTrack_id(event.getMDCPropertyMap().get("TRACE_ID"));
            log.setLog_content(event.getFormattedMessage());
            logService = SpringUtil.getBean(LogService.class);
            logService.saveLog(log);
            return FilterReply.ACCEPT;
        }else {
            return FilterReply.DENY;
        }
    }
}

